<?php

/**
 * Product form.
 *
 * @package    jobeet
 * @subpackage form
 * @author     Your name here
 * @version    SVN: $Id: ProductForm.class.php 7 2011-04-20 11:17:06Z Inoryy@gmail.com $
 */
class ProductForm extends BaseProductForm
{
	public $languages = array();
	
	public static $multiLangFields = array( 	'name' => array( 'title'=> 'Name' ),
												//'volume' => array('title' => 'Volume'),
		 										'description' => array( 'title' => 'Description' ), 
												 
	 );
  public function configure()
  {
  	unset($this['created_at']); 
  	unset($this['updated_at']);
  	unset($this['pri']);
  	unset($this['connected_products_list']); 

  
	
  	$this->configureLanguages();
  	
  	
  	# product groups
  	$prodGroups = $this->getProductGroupsLedder();

	
   $this->widgetSchema['connections'] =  new sfWidgetFormSelectMany(array(
  		'choices'  => $prodGroups,
		),  array('name' => 'connections[]', 'size' => 5, 'class' => 'formInput') );

   $this->widgetSchema['parameter'] =  new sfWidgetFormSelect(array(
  		'choices'  => array(
				'default' => 'Tavaline',
				'roof_product' => 'Katuse toode'),

		),  array('size' => 1, 'class' => 'formInput') );

   $this->validatorSchema['parameter'] = new sfValidatorString(array('required' => false));

	
	if( $this->getObject() ){
		$selectedprodGroups = array();
		foreach( $this->getObject()->getProductGroups() as $rel ){
			$selectedprodGroups[] = $rel->group_id;	
		}
		$this->widgetSchema['connections']->setDefault( $selectedprodGroups );
			
	}

	
	 
	# connected products
	$this->widgetSchema['connected_products'] = new sfWidgetFormTextarea(array(),array('name' => 'connected_products', 'rows'=> 2, 'cols' => '30', 'class' => 'formInput'));

	
	
  }
  
  public function configureLanguages(){
  	
  	$this->embedI18n( $this->getLanguagesAbr() );  
  	$this->languages = $this->getLanguages();
  	
  	foreach($this->getLanguagesAbr() as $abr){
  		$this->widgetSchema[$abr]['description'] = new sfWidgetFormTextarea();
  	}
  }
  
  
  public function getLanguagesAbr(){
  	    $languages = array();
  	foreach( $this->getLanguages() as $lang ){
  		$languages[] = $lang['abr'];
  	}
  	
  	return $languages;
  }
  
  public function getLanguages()
  {
  	return Doctrine::getTable('Language')->findAll();
  }
  
  
  public function getProductGroupsLedder(){
  	
  	$prodGroups = array();
  	
	foreach( Doctrine::getTable('ProductGroup')->findAll() as $pGroup ){
		if( $pGroup->title != 'root' )
			$prodGroups[$pGroup->getId()] = str_repeat('-> ', $pGroup->getLevel()-1) . $pGroup->title;
	}

	return $prodGroups;
  	
  }
  
  
}
